package com.itestor.iptv.modules.common.actions;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import com.itestor.iptv.global.actions.AbstractPagedQueryAction;
import com.itestor.iptv.global.bean.AbstractPagedQuery;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.service.UserService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.UserQuery;

/**
 * 
 * @author Mr.li
 * 
 */
public class UserQueryAction extends AbstractPagedQueryAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(UserQueryAction.class);

    private UserService userService;

    @SuppressWarnings("unchecked")
    @Override   
    public AbstractPagedQuery doPagedQuery(ActionForm form,
            HttpServletRequest request, int pagerCount, int pagerOffset)
            throws Exception {
        String username = request.getParameter("username");
        
        LoggerUtils.logActionInfo("Query the user"
                + (StringUtils.isEmpty(username) ? (" - Username:" + username)
                        : "") + ".", logger, request);

        UserQuery userQuery = new UserQuery();
        userQuery.setPagerCount(pagerCount);
        userQuery.setPagerOffset(pagerOffset);
        if (!StringUtils.isEmpty(username)) {
            userQuery.setUsername(username);
        }
        
        userService.queryUserList(userQuery);
        
        request.setAttribute(CommonConstant.REQUEST_ATTR_Q_USER, userQuery);
        request.setAttribute(CommonConstant.REQUEST_ATTR_ROLES, userService
                .getRoleMap());
        return userQuery;
    }

    /**
     * @param pUserService
     */
    public void setUserService(UserService pUserService) {
        userService = pUserService;
    }

}